﻿using B.Auth.APi.Application.Command.Role;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand.Role
{
    public class UpdateRoleCommandHand : IRequestHandler<UpdateRoleCommand, ApiResult<int>>
    {

        private readonly IBaseRepository<RoleModel> db;

        public UpdateRoleCommandHand(IBaseRepository<RoleModel> db)
        {
            this.db = db;
        }

        public Task<ApiResult<int>> Handle(UpdateRoleCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();
            var user = new RoleModel();
            user.Id = request.Id;
            user.RoleName = request.RoleName;
            user.Del = request.Del;
            user.CreateUserName = "张三";

            user.CreateTime = request.CreateTime;
            user.UpdateTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            user.UodateUserName = "张三";
           

            var useshow = db.GetAll().Where(x => x.RoleName == request.RoleName && x.Id != request.Id&&x.Del==0).Count();
            if (useshow == 1)
            {

                result.Message = "用户名不能重复";
                result.Code = ApiEnum.fail;
                return Task.FromResult(result);

            }

            db.Update(user);
            result.Code = ApiEnum.ok;
            result.Message = "成功";
            return Task.FromResult(result);
        }
    }
}
